<?php
// $Id: qupload.class.php 11 2011-03-19 05:36:12Z tomqin@gmail.com $

/**
 * 文件上传
 *
 * @author TomQin <tomqin@gmail.com>
 * @copyright Copyright (c) 2009-2012 SupeFun.Com Inc.
 * @package Core
 */


define('UPLOAD_ERR_FILE_SIZE',  10);
define('UPLOAD_ERR_FILE_TYPE',  11);
define('UPLOAD_ERR_IMAGE_TYPE', 12);
define('UPLOAD_ERR_IMAGE_SIZE', 13);
define('UPLOAD_ERR_UNKNOWN',    99);

class QUpload {

	public $file;	// $_FILE['file'];
	private $options = array(
		'max_length'  => 0,				//0,无限制;否则字节
		'allow_type'  => 'image',		//可以取文件名后缀
		'check_image' => false,
		'image_min_width'  => 0,
		'image_max_width'  => 10000,
		'image_min_height' => 0,
		'image_max_height' => 10000,
	);

	public function __construct($file, $options = array()){
		$this->file = $file;
		foreach($this->options as $key=>$var)
		{
			if(isset($options[$key]))
				$this->options[$key] = $options[$key];
		}
	}

	public function uploadFile(){
		$this->file['error'] = $this->checkUpload();
		return $this->file['error'];
	}

	/*

	*/
	private function checkUpload(){
		if(!isset($this->file))
		{
			return UPLOAD_ERR_NO_FILE;
		}

		if($this->file['error'])
		{
			return $this->file['error'];
		}

		if(empty($this->file['tmp_name']) || empty($this->file['size']))
		{
			return UPLOAD_ERR_NO_FILE;
		}

		//文件尺寸
		if($this->options['max_length'] > 0 && $this->file['size'] > $this->options['max_length'])
		{
			return UPLOAD_ERR_FILE_SIZE;
		}

		//文件类型
		if($this->options['allow_type'])
		{
			$fileext = strtolower(pathinfo($this->file['name'], PATHINFO_EXTENSION));

			$allow_type = ($this->options['allow_type'] == 'image') ? 'jpeg|jpg|jpge|gif|png' : $this->options['allow_type'];
			if(!preg_match("/{$allow_type}/i", $fileext))
			{
				return UPLOAD_ERR_FILE_TYPE;
			}
		}

		//检查图片尺寸
		if($this->options['check_image'])
		{
			return $this->checkImage();
		}

		return UPLOAD_ERR_OK;
	}

	private function checkImage()
	{
		$this->file['imageinfo'] = getimagesize($this->file['tmp_name']);
		if(!$this->file['imageinfo'])
		{
			return UPLOAD_ERR_IMAGE_TYPE;
		}
		else
		{
			if($this->options['check_image'])
			{
				list($width, $height, $type, $attr) = $this->file['imageinfo'];
				if($width  <= $this->options['image_min_width'] ||
				   $width  >= $this->options['image_max_width'] ||
				   $height <= $this->options['image_min_height'] ||
				   $height >= $this->options['image_max_height'])
				{
					return UPLOAD_ERR_IMAGE_SIZE;
				}
			}
		}

		return UPLOAD_ERR_OK;
	}

	public function getError($no){
		$error = array(
			UPLOAD_ERR_OK         => '文件上传成功', //0
			UPLOAD_ERR_INI_SIZE   => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',	//1
			UPLOAD_ERR_FORM_SIZE  => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
			UPLOAD_ERR_PARTIAL    => 'The uploaded file was only partially uploaded.',
			UPLOAD_ERR_NO_FILE    => '没有文件上传',
			UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
			UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
			UPLOAD_ERR_EXTENSION  => 'A PHP extension stopped the file upload.',

			UPLOAD_ERR_FILE_SIZE  => 'UPLOAD_ERR_FILE_SIZE',
			UPLOAD_ERR_FILE_TYPE  => 'UPLOAD_ERR_FILE_TYPE',
			UPLOAD_ERR_IMAGE_TYPE => 'UPLOAD_ERR_IMAGE_TYPE',
			UPLOAD_ERR_IMAGE_SIZE => 'UPLOAD_ERR_IMAGE_SIZE',
			UPLOAD_ERR_UNKNOWN    => 'UPLOAD_ERR_UNKNOWN',
		);

		if(isset($error[$no]))
			return $error[$no];
		else
			return $error[UPLOAD_ERR_UNKNOWN];
	}
}
?>